package userManage.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import userManage.Model.dao.JsonUntil;
import userManage.Model.service.serviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet(name = "agreeReturnBook", value = "/manage/userManage/agreeReturnBook")
public class agreeReturnBookservlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转化成String类
        String jsonString = String.valueOf(JsonUntil.getJson(req));
        //转换成JSONObject类
        JSONObject jsonObject = JSON.parseObject(jsonString);
        //获取成员姓名,借阅图书以及编号
        String userName = jsonObject.getString("userName");
        String bookName = jsonObject.getString("bookName");
        int bookId = jsonObject.getInteger("bookId");

        //进行同意还书操作
        int count = 0;
        try {
            count = serviceImpl.agreeReturnBook(userName, bookName, bookId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //向前端返回值
        PrintWriter out = resp.getWriter();
        JSONObject object = new JSONObject();
        //如果返回值为0
        if (count == 0) {
            object.put("code", "-1");
            object.put("message", "不批准还书");
            out.println(object);
        } else {
            object.put("code", "200");
            object.put("message", "批准还书");
            out.println(object);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
